<?php
/**
 * 根据表名生成 model 文件
 * @param $s_table_name
 */
function make_model_file($s_table_name)
{
    $s_file_path = LARAVEL_APP_DIR . '/app/Common/Model/';
    $s_file_hump_name = string_underline_to_hump($s_table_name);
    $s_file_name = $s_file_hump_name;
    $s_file_full_name = $s_file_path . $s_file_name . '.php';
    $res = is_file($s_file_full_name);
    //文件数据
    $s_file_data = '<?php
/**
 * common model file Created by PhpStorm.
 * User: yang
 * Date: ' . date('Y/m/d') . '
 */

namespace Common\Model;

class ' . $s_file_hump_name . ' extends Base
{
    protected $table = \'' . $s_table_name . '\';
    protected $guarded = [];
    
    //public $timestamps = false;

}';

    if (!$res) {
        $result = file_put_contents($s_file_full_name, $s_file_data);
        if ($result === false) {
            echo 'Model文件生成失败';
            die;
        }
    }
}

/**
 * 根据表名生成 repository 文件
 * @param $s_table_name
 */
function make_repository_file($s_table_name)
{
    $s_file_path = LARAVEL_APP_DIR . '/app/Common/Repository/';
    $s_file_hump_name = string_underline_to_hump($s_table_name);
    $s_file_name = $s_file_hump_name . 'Repository';
    $s_file_model_name = $s_file_hump_name;
    $s_file_full_name = $s_file_path . $s_file_name . '.php';
    $res = is_file($s_file_full_name);
    //文件数据
    $s_file_data = '<?php
/**
 * common repository file Created by PhpStorm.
 * User: yang
 * Date: ' . date('Y/m/d') . '
 */
namespace Common\Repository;

use Common\Model\\' . $s_file_hump_name . ';

class ' . $s_file_name . ' extends BaseRepository
{
    public function baseModel(){
        return new ' . $s_file_hump_name . '();
    }

}';

    if (!$res) {
        $result = file_put_contents($s_file_full_name, $s_file_data);
        if ($result === false) {
            echo 'Repository 文件生成失败';
            die;
        }
    }
}

/**
 * 根据表名生成 service 文件
 * @param $s_table_name
 */
function make_service_file($s_table_name)
{
    $s_file_path = LARAVEL_APP_DIR . '/app/Http/Service/Admin/';
    $s_file_hump_name = string_underline_to_hump($s_table_name);
    $s_file_name = $s_file_hump_name . 'Service';
    $s_file_model_name = $s_file_hump_name . 'Repository';
    $s_file_full_name = $s_file_path . $s_file_name . '.php';
    $res = is_file($s_file_full_name);
    //文件数据
    $s_file_data = '<?php
/**
 * admin service file Created by PhpStorm.
 * User: yang
 * Date: ' . date('Y/m/d') . '
 */
namespace App\Http\Service\Admin;

use Common\Repository\\' . $s_file_model_name . ';

class ' . $s_file_name . '
{
    /**
     * 获取列表
     * @param array $get_data
     * @return array
     */
    public function getList($get_data = [])
    {
        try {
            if(isset($get_data[\'field\']) && isset($get_data[\'order\'])){
                $arr_option[\'order\'] = [\'key\'=>$get_data[\'field\'],\'value\'=>$get_data[\'order\']];
            }
            $arr_option[\'limit\'] = $get_data[\'limit\'];
            $arr_option[\'where\'] = $get_data[\'where\'];
            
            $result = ' . $s_file_model_name . '::getList($arr_option);
            $list = list_data_page($result);

            return $list;
        } catch (\Exception $exception) {
            return hello_error(\'失败！\',$exception->getMessage());
        }
    }

    /**
     * 添加操作
     * @param array $post_data
     * @return array
     */
    public function insert($post_data = [])
    {
        try {
            $result = ' . $s_file_model_name . '::addOne($post_data);

            if ($result) {
                return hello_success(\'添加成功！\', $result);
            }

            return hello_error(\'添加失败！\');
        } catch (\Exception $exception) {
            return hello_error(\'失败！\',$exception->getMessage());
        }
    }

    /**
     * 编辑视图
     * @param int $id
     * @return array
     */
    public function edit(int $id)
    {
        try {
            if ($id == 0) {
                error_prompt(\'参数错误\');
            }
            $result = ' . $s_file_model_name . '::findOne($id);

            if ($result) {
                return hello_success(\'获取成功！\', $result);
            }

            return hello_error(\'获取失败！\');
        } catch (\Exception $exception) {
            return hello_error(\'失败！\',$exception->getMessage());
        }
    }

    /**
     * 编辑操作
     * @param array $post_data
     * @return array
     */
    public function update($post_data = [])
    {
        try {
            $result = ' . $s_file_model_name . '::editOne($post_data);

            if ($result) {
                return hello_success(\'编辑成功！\', $result);
            }

            return hello_error(\'编辑失败！\');
        } catch (\Exception $exception) {
            return hello_error(\'失败！\',$exception->getMessage());
        }
    }

    /**
     * 删除操作
     * @param int $id
     * @return array
     */
    public function delete(int $id)
    {
        try {
            if ($id == 0) {
                error_prompt(\'参数错误\');
            }
            $result = ' . $s_file_model_name . '::deleteOne($id);

            if ($result) {
                return hello_success(\'删除成功！\', $result);
            }

            return hello_error(\'删除失败失败！\');
        } catch (\Exception $exception) {
            return hello_error(\'失败！\',$exception->getMessage());
        }
    }
    
    /**修改状态
     * @param array $post_data
     * @return array
     */
    public function editStatus($post_data = [])
    {
        try {
            $up_status = [
                \'id\' => $post_data[\'id\'],
                \'status\' => $post_data[\'status\']==1 ? 0 : 1
            ];

            $result = ' . $s_file_model_name . '::editOne($up_status);

            if ($result) {
                return hello_success(\'修改成功！\', $up_status[\'status\']);
            }

            return hello_error(\'修改失败！\');
        } catch (\Exception $exception) {
            return hello_error(\'失败！\', $exception->getMessage());
        }
    }
    
}';

    if (!$res) {
        $result = file_put_contents($s_file_full_name, $s_file_data);
        if ($result === false) {
            echo 'service 文件生成失败';
            die;
        }
    }
}

/**
 * 根据表名生成 controller 文件
 * @param $s_table_name
 */
function make_controller_file($s_table_name)
{
    $s_file_path = LARAVEL_APP_DIR . '/app/Http/Controllers/Admin/';
    $s_file_hump_name = string_underline_to_hump($s_table_name);
    $s_file_name = $s_file_hump_name . 'Controller';
    $s_file_model_name = $s_file_hump_name . 'Service';
    $s_file_full_name = $s_file_path . $s_file_name . '.php';
    $res = is_file($s_file_full_name);
    //文件数据
    $s_file_data = '<?php
/**
 * admin service file Created by PhpStorm.
 * User: yang
 * Date: ' . date('Y/m/d') . '
 */
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Service\Admin\\' . $s_file_model_name . ';

class ' . $s_file_name . ' extends Controller
{
    /**
     * 视图层
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view(\'Admin.' . $s_table_name . '.index\');
    }
    
    /**
     * 获取列表数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $get_data = $request->all();
    
        $result = ' . $s_file_model_name . '::getList($get_data);
    
        return response()->json($result);
    }
    
    /**
     * 添加视图
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view(\'Admin.' . $s_table_name . '.create\');
    }
    
    /**
     * 添加操作
     * @param Request $request
     * @return array
     */
    public function insert(Request $request)
    {
        $post_data = $request->except([\'_token\']);
    
        return ' . $s_file_model_name . '::insert($post_data);
    }
    
    /**
     * 编辑视图
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $id = $request->get(\'id\', 0);
    
        $result = ' . $s_file_model_name . '::edit((int)$id);
    
        return view(\'Admin.' . $s_table_name . '.edit\', $result);
    }
    
    /**
     * 编辑操作
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $post_data = $request->except([\'_token\']);
    
        return ' . $s_file_model_name . '::update($post_data);
    }
    
    /**
     * 删除操作
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $id = $request->get(\'id\', 0);
    
        return ' . $s_file_model_name . '::delete((int)$id);
    }
    
    /**修改状态
     * @param Request $request
     * @return array
     */
    public function status(Request $request)
    {
        $post_data = $request->only([\'id\', \'status\']);

        return ' . $s_file_model_name . '::editStatus($post_data);
    }
}';

    if (!$res) {
        $result = file_put_contents($s_file_full_name, $s_file_data);
        if ($result === false) {
            echo 'controller 文件生成失败';
            die;
        }
    }
}

/**
 * 根据表名生成对应的各种文件 目前包括 model repository service [controller]
 * @param $s_xiahua_name
 */
function make_file($s_xiahua_name)
{
    //生成 common model 文件
    make_model_file($s_xiahua_name);

    //生成 common repository 文件
    make_repository_file($s_xiahua_name);

    //生成 admin service 文件
    make_service_file($s_xiahua_name);

    //生成 admin controller 文件
    make_controller_file($s_xiahua_name);
}

/**
 * 批量生产数据库所有表对应的laravel_model和laravel_service文件
 * @return string
 */
function creat_common_file()
{
    $db_prefix = env('DB_PREFIX', '');
    $db_name = env('DB_DATABASE', '');
    $tables = DB::select('show tables');

    foreach ($tables as $value) {
        $s_replace_name = 'Tables_in_' . $db_name;
        $s_xiahua_name = str_replace($db_prefix, '', $value->$s_replace_name);
        make_one_table_file($s_xiahua_name);
    }

    return 'ok';
}

/**
 * 字符串命名风格转换 【下划线转驼峰】
 * @param $string
 * @param bool $ucfirst
 * @param string $delimiters
 * @return mixed|string
 */
function string_underline_to_hump($string, $ucfirst = true, $delimiters = '_')
{
    if (is_string($string)) {
        //字符串里单词首字母大写 然后将分割字符去除
        $string = str_replace($delimiters, '', ucwords($string, $delimiters));

        //字符串首字母是否大写
        if (!$ucfirst) {
            $string = lcfirst($string);
        }
    }
    return $string;
}